package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {
    /**
     * 插入订单基本数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> list(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 根据状态和查询时间查询超时订单
     * @param pendingPayment
     * @param timeout
     * @return
     */
    @Select("select * from orders where status = #{pendingPayment} and order_time < #{timeout}")
    List<Orders> selectByStatusAndOrderTime(Integer pendingPayment, LocalDateTime timeout);

    /**
     * 根据订单时间和状态统计每日营业额
     * @param map
     * @return
     */
    @Select("select sum(amount) from orders where status = #{status} and order_time between  #{beginTime} and #{endTime}")
    Double sumByMap(Map<String, Object> map);

    /**
     * 统计订单数量
     * @param map
     */
    Integer countByMap(Map<String, Object> map);

    /**
     * 统计销量排名top10·
     * @return
     */
    @MapKey("name")
    List<Map<String,Object>> sumTop10(Map<String, Object> map);
}


